package com.example.event_resource_java.trainapi.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.event_resource_java.commons.domain.UserDO;
import com.example.event_resource_java.oss.util.BuildTree;
import com.example.event_resource_java.oss.util.Tree;
import com.example.event_resource_java.trainapi.dao.DictDO;
import com.example.event_resource_java.trainapi.mapper.DictMapeer;
import com.example.event_resource_java.trainapi.service.DictService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <pre>
 * </pre>
 * <small> 2018年3月23日 | Aron</small>
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapeer, DictDO> implements DictService {
    @Override
    public List<DictDO> listType() {
        return baseMapper.listType();
    }

    @Override
    public String getName(String type, String value) {
        QueryWrapper<DictDO> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("value", value);
        DictDO selectOne = DictDO.dao.selectOne( wrapper);
        return selectOne == null ? "" : selectOne.getName();
    }

    @Override
    public List<DictDO> getHobbyList(UserDO userDO) {
        QueryWrapper<DictDO> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "hobby");
        List<DictDO> hobbyList = DictDO.dao.selectList(wrapper);

        if (StringUtils.isNotEmpty(userDO.getHobby())) {
            String userHobbys[] = userDO.getHobby().split(";");
            for (String userHobby : userHobbys) {
                for (DictDO hobby : hobbyList) {
                    if (!Objects.equals(userHobby, hobby.getId().toString())) {
                        continue;
                    }
                    hobby.setRemarks("true");
                    break;
                }
            }
        }

        return hobbyList;
    }

    @Override
    public List<DictDO> getSexList() {
        QueryWrapper<DictDO> wrapper = new QueryWrapper<>();
        wrapper.eq("type", "sex");
        return DictDO.dao.selectList(wrapper);
    }

    @Override
    public List<DictDO> selectListBy(Map map) {
        return baseMapper.selectListBy(map);
    }

    @Override
    public List<DictDO> selectListAll() {
        return baseMapper.selectListAll();
    }

    @Override
    public List<Long> selPid(Long pid) {
        return baseMapper.selPid(pid);
    }
    @Override
    public String selPName(Long id) {
        return baseMapper.selPName(id);
    }

    @Override
    public List<DictDO> selDicType() {
        return baseMapper.selDicType();
    }

    @Override
    public String selDicBy(Map map) {
        return baseMapper.selDicBy(map);
    }

    @Override
    public List<Map> selTypeNameVue(Map map) {
        return baseMapper.selTypeNameVue(map);
    }

    @Override
    public List<Map> selByTypeVue(String type){
        return baseMapper.selByTypeVue(type);
    }

    @Override
    public Tree<DictDO> getTree() {
        List<Tree<DictDO>> trees = new ArrayList<Tree<DictDO>>();
        Wrapper<DictDO> wrapper = new QueryWrapper<DictDO>().orderByDesc("id");
        List<DictDO> depts = baseMapper.selectList(wrapper);
        Long[] pDepts = baseMapper.listParentDept();
        Long[] uDepts = baseMapper.listAllDept();
        Long[] allDepts = (Long[]) ArrayUtils.addAll(pDepts, uDepts);
        for (DictDO dept : depts) {
            if (!ArrayUtils.contains(allDepts, dept.getId())) {
                continue;
            }
            Tree<DictDO> tree = new Tree<DictDO>();
            tree.setId(dept.getId().toString());
            tree.setParentId(dept.getParentId().toString());
            tree.setText(dept.getName());
            Map<String, Object> state = new HashMap<>(16);
            state.put("opened", true);
            state.put("mType", "dept");
            tree.setState(state);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        Tree<DictDO> t = BuildTree.build(trees);
        return t;
    }

}
